package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.leaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.errorprone.annotations.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
//    @Autowired
//    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public Page<ApartmentItemVo> pageApartmentItemVo(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return baseMapper.pageApartmentItemVoMapper(apartmentItemVoPage,queryVo);
    }



    @Override
    public void saveOrUpdateSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        Long provinceId = apartmentSubmitVo.getProvinceId();
        ProvinceInfo provinceInfo = provinceInfoService.getById(provinceId);
        String provinceInfoName = provinceInfo.getName();
        apartmentSubmitVo.setProvinceName(provinceInfoName);

        Long cityId = apartmentSubmitVo.getCityId();
        CityInfo cityInfo = cityInfoService.getById(cityId);
        String cityInfoName = cityInfo.getName();
        apartmentSubmitVo.setCityName(cityInfoName);
        Long districtId = apartmentSubmitVo.getDistrictId();
        DistrictInfo districtInfo = districtInfoService.getById(districtId);
        String districtInfoName = districtInfo.getName();
        apartmentSubmitVo.setDistrictName(districtInfoName);


        //获取公寓的id，作为关联表中存储的公寓编号
        Long id = apartmentSubmitVo.getId();

        if (id!=null){
            //更新操作
            //将当前公寓所关联的所有数据，删除掉
            //1.删除公寓配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //2.删除公寓标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //3，删除公寓杂费信息
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));

            //4，删除公寓图片信息
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id));

        }
//        先保存公寓的基本信息，虽然传得是VO，但是VO是ApartmentInfo的子类，包含了基本信息
        saveOrUpdate(apartmentSubmitVo);
        //2.保存公寓配套信息    apartment_facility
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //判断公寓配套的id集合是否为空
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            //创建公寓和配套关联的集合
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            //遍历facilityInfoIds集合，取出配套的id
            for (Long facilityInfoId : facilityInfoIds) {
                //创建ApartmentFacility对象
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                apartmentFacilities.add(apartmentFacility);
            }
            //批量保存到关系表中
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

        //3.保存公寓标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        //判断标签信息是否为空
        if(!CollectionUtils.isEmpty(labelIds)){
            //创建公寓信息和标签信息的关联集合
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            //遍历labelIds集合，取出配套的id
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //4,保存公寓杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> apartmentFeeValueArrayList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                apartmentFeeValueArrayList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueArrayList);
        }

        //5.保存公寓图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                //构建GraphInfo对象
                GraphInfo graphInfo = new GraphInfo();
                //设置图片名称
                graphInfo.setName(graphVo.getName());
                //设置图片所属类型
                graphInfo.setItemType(ItemType.APARTMENT);
                //设置图片所属的对象id  公寓或房间的id
                graphInfo.setItemId(id);

                //设置图片的url
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);

            }
            graphInfoService.saveBatch(graphInfoList);
        }

    }

    //根据id删除公寓信息
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Override
    public void removeApartmentInfoById(Long id) {
        //判断公寓内是否有房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        List<RoomInfo> list = roomInfoService.list(roomInfoLambdaQueryWrapper);
//        long roomCount = roomInfoService.count(roomInfoLambdaQueryWrapper);
        Long roomCount = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);
        if(list.isEmpty()){
            throw new leaseException(ResultCodeEnum.APARTMENT_HAS_ROOM);
        }
        else{
            this.removeById(id);
            //删除关联数据
            //1.删除公寓配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //2.删除公寓标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //3，删除公寓杂费信息
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));

            //4，删除公寓图片信息
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id));

        }

    }
@Autowired
private GraphInfoMapper graphInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Override
    //查询公寓详细信息
    public ApartmentDetailVo getApartmentDetailVOById(Long id) {
        //1.根据id查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        //2.根据公寓id和图片类型id查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVOByApartmentIdAndItemTypeId(id,ItemType.APARTMENT);
        //3.根据公寓id查询公寓配套
        List<FacilityInfo> facilityInfoList =  facilityInfoMapper.selectFacilityByApartmentId(id);
        //4.根据公寓id查询公寓标签
        List<LabelInfo> labelInfoList =  labelInfoMapper.selectLableByApartmentId(id);
        //5.根据公寓id查询公寓杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVOListByApartmentId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        //设置图片信息
        apartmentDetailVo.setGraphVoList(graphVoList);
        //设置配套信息
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        //设置标签
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //设置杂费
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }
}




